/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2023-10-31
 * Time: 16:11
 */
public class Test {

    public static void main(String[] args) {

    }

    //递归求斐波那契数列的第 N 项
    public static int fib(int n){
        if(n < 3){
            return 1;
        }else{
            return fib(n-2) + fib(n-1);
        }
    }

    public static void main7(String[] args) {
        System.out.println(fib(7));
    }

    // 写一个递归方法，输入一个非负整数，返回组成它的数字之和
    public static int sum2(int n){
        if(n < 10){
            return n;
        }else{
            return n % 10 + sum2(n / 10);
        }
    }

    public static void main6(String[] args) {
        System.out.println(sum2(12345));
    }

    //递归打印数字的每一位
    public static void print(int n){
        if(n < 10){
            System.out.println(n);
        }else{
            print(n / 10);
            System.out.println(n % 10);
        }
    }
    public static void main5(String[] args) {
        print(1234);
    }

    //求阶乘
    public static int fac(int n){
        if(n == 1){
            return 1;
        }else{
            return n * fac(n-1);
        }
    }

    public static void main4(String[] args) {
        System.out.println(fac(5));
    }
    //求和
    public static int sum(int n){
        if(n == 1){
            return 1;
        }else{
            return n + sum(n-1);
        }
    }
    public static void main3(String[] args) {
        System.out.println(sum(10));
    }

    //汉诺塔
    public static void hanoi(int n,char pos1,char pos2,char pos3) {
        if(n == 1) {
            move(pos1,pos3);
            return;
        }
        hanoi(n-1,pos1,pos3,pos2);
        move(pos1,pos3);
        hanoi(n-1,pos2,pos1,pos3);
    }

    public static void move(char pos1,char pos2) {
        System.out.print(pos1+ "-> "+pos2+" ");
    }
    public static void main2(String[] args) {

        hanoi(3, 'A','B', 'C');

    }
}
